package com.sg.service.biz.app.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.app.req.QueryPlanTargetAccountListReq;
import com.sg.dto.biz.app.req.ReleaseBatchHandlePlanAccountUpdStatusComReqDto;
import com.sg.dto.biz.app.req.UndoBatchHandlePlanAccountUpdStatusComReqDto;
import com.sg.dto.biz.app.res.ReleaseBatchHandlePlanAccountUpdStatusComRespDto;
import com.sg.dto.biz.app.res.UndoBatchHandlePlanAccountUpdStatusComRespDto;
import com.sg.entity.OmsPlanTargetAccount;
import com.sg.service.base.app.MOmsPlanTargetAccountService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class PlanTargetAccountServiceImpl implements com.wicket.okrapp.biz.service.PlanTargetAccountService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MOmsPlanTargetAccountService mOmsPlanTargetAccountService;

    /**
     * D3-9发布批次处理规划账户更新状态(公共)[3620]
     * gen by moon at 10/27/2022, 4:16:44 PM
     */
    @Trace(operationName = "D3-9发布批次处理规划账户更新状态(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ReleaseBatchHandlePlanAccountUpdStatusComRespDto releaseBatchHandlePlanAccountUpdStatusCom(ReleaseBatchHandlePlanAccountUpdStatusComReqDto reqDto) {


        //步骤0: 3-9-01查规划目标账户列表 - queryPlanTargetAccountList
        List<OmsPlanTargetAccount> listOmsPlanTargetAccount = new ArrayList<>();
        QueryPlanTargetAccountListReq queryPlanTargetAccountListReq = new QueryPlanTargetAccountListReq();
        queryPlanTargetAccountListReq.setUpdateStatus("NEW");//sourceId:162813_1
        if (reqDto != null) {
            queryPlanTargetAccountListReq.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:162825_1
            queryPlanTargetAccountListReq.setBatchCode(reqDto.getBatchCode());//SimpleFieldAssign//sourceId:162816_1
        }

        /*3-9-01查本次新增规划目标账户列表[2738]   */
        Assert.isNull(queryPlanTargetAccountListReq.getUpdateStatus(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次新增规划目标账户列表-批次下更新状态不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq.getEvaluationTemplateId(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次新增规划目标账户列表-冗余评价模板ID不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq.getBatchCode(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次新增规划目标账户列表-批次标识不能为空", false);
        listOmsPlanTargetAccount = mOmsPlanTargetAccountService.queryPlanTargetAccountList(queryPlanTargetAccountListReq)/*vcase invoke 本地 method 方法调用;*/;


//步骤1: 3-9-01批量改规划目标账户 - batchUpdatePlanTargetAccount
        boolean bOOLEAN;
        if (listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount) && listOmsPlanTargetAccount.size() > 0) {
            List<OmsPlanTargetAccount> listOmsPlanTargetAccount_2 = new ArrayList<>();
            if (listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount) && listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount)) {
                listOmsPlanTargetAccount_2 = listOmsPlanTargetAccount.stream().map(item -> {
                    OmsPlanTargetAccount elm = BeanUtil.toBean(item, OmsPlanTargetAccount.class);
                    elm.setIsUpdate("FALSE");
                    elm.setBatchStartTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
                    elm.setUpdateStatus("UNCHANGED");
                    elm.setIsEditing("FALSE");
                    elm.setSubjectLifeCycle("CURRENTLY_VALID");
                    elm.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
                    elm.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME

                    return elm;
                }).collect(Collectors.toList());//objList-to-objLists//sourceId:162917_1
            }

            /*3-9-01批量改规划目标账户当前有效[3630]   */

            bOOLEAN = mOmsPlanTargetAccountService.batchUpdatePlanTargetAccount(listOmsPlanTargetAccount_2)/*vcase invoke 本地 method 方法调用;*/;


        }

//步骤2: 3-9-01查规划目标账户列表 - queryPlanTargetAccountList
        List<OmsPlanTargetAccount> listOmsPlanTargetAccount_3 = new ArrayList<>();
        QueryPlanTargetAccountListReq queryPlanTargetAccountListReq_1 = new QueryPlanTargetAccountListReq();
        queryPlanTargetAccountListReq_1.setUpdateStatus("ARCHIVE");//sourceId:162848_1
        if (reqDto != null) {
            queryPlanTargetAccountListReq_1.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:162846_1
            queryPlanTargetAccountListReq_1.setBatchCode(reqDto.getBatchCode());//SimpleFieldAssign//sourceId:162847_1
        }

        /*3-9-01查本次存档规划目标账户（用于想存档）[2738]   */
        Assert.isNull(queryPlanTargetAccountListReq_1.getEvaluationTemplateId(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-冗余评价模板ID不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_1.getBatchCode(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-批次标识不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_1.getUpdateStatus(), "D3-9发布批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-批次下更新状态不能为空", false);
        listOmsPlanTargetAccount_3 = mOmsPlanTargetAccountService.queryPlanTargetAccountList(queryPlanTargetAccountListReq_1)/*vcase invoke 本地 method 方法调用;*/;


//步骤3: 3-9-01批量改规划目标账户 - batchUpdatePlanTargetAccount
        boolean bOOLEAN_1;
        if (listOmsPlanTargetAccount_3 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_3) && listOmsPlanTargetAccount_3.size() > 0) {
            List<OmsPlanTargetAccount> listOmsPlanTargetAccount_5 = new ArrayList<>();
            if (listOmsPlanTargetAccount_3 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_3) && listOmsPlanTargetAccount_3 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_3)) {
                listOmsPlanTargetAccount_5 = listOmsPlanTargetAccount_3.stream().map(item -> {
                    OmsPlanTargetAccount elm = BeanUtil.toBean(item, OmsPlanTargetAccount.class);
                    elm.setIsUpdate("FALSE");
                    elm.setBatchEndtTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
                    elm.setArchivngType("ARCHIVED");
                    elm.setIsArchive("TRUE");
                    elm.setSubjectLifeCycle("ARCHIVING");
                    elm.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
                    elm.setArchiveTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME

                    return elm;
                }).collect(Collectors.toList());//objList-to-objLists//sourceId:162781_1
            }

            /*3-9-01批量改规划目标账户为历史存档[3630]   */

            bOOLEAN_1 = mOmsPlanTargetAccountService.batchUpdatePlanTargetAccount(listOmsPlanTargetAccount_5)/*vcase invoke 本地 method 方法调用;*/;


        }

        ReleaseBatchHandlePlanAccountUpdStatusComRespDto retData = new ReleaseBatchHandlePlanAccountUpdStatusComRespDto();


        return retData;
    }

    /**
     * D3-9撤销批次处理规划账户更新状态(公共)[3621]
     * gen by moon at 5/8/2023, 6:28:14 AM
     */
    @Trace(operationName = "D3-9撤销批次处理规划账户更新状态(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UndoBatchHandlePlanAccountUpdStatusComRespDto undoBatchHandlePlanAccountUpdStatusCom(UndoBatchHandlePlanAccountUpdStatusComReqDto reqDto) {


        //步骤0: 3-9-01查规划目标账户列表 - queryPlanTargetAccountList
        List<OmsPlanTargetAccount> listOmsPlanTargetAccount = new ArrayList<>();
        QueryPlanTargetAccountListReq queryPlanTargetAccountListReq = new QueryPlanTargetAccountListReq();
        queryPlanTargetAccountListReq.setAccountAttributionObjType("TARGET_CAT");//sourceId:162992_1
        queryPlanTargetAccountListReq.setUpdateStatus("NEW");//sourceId:162995_1
        if (reqDto != null) {
            queryPlanTargetAccountListReq.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:162997_1
            queryPlanTargetAccountListReq.setBatchCode(reqDto.getBatchCode());//SimpleFieldAssign//sourceId:162996_1
        }

        /*3-9-01查本次新增目标分类账户列表[2738]   */
        Assert.isNull(queryPlanTargetAccountListReq.getAccountAttributionObjType(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次新增目标分类账户列表-账户归属对象类型不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq.getEvaluationTemplateId(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次新增目标分类账户列表-冗余评价模板ID不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq.getBatchCode(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次新增目标分类账户列表-批次标识不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq.getUpdateStatus(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次新增目标分类账户列表-批次下更新状态不能为空", false);
        listOmsPlanTargetAccount = mOmsPlanTargetAccountService.queryPlanTargetAccountList(queryPlanTargetAccountListReq)/*vcase invoke 本地 method 方法调用;*/;


//步骤1: 3-9-01批量删规划目标账户 - batchDeletePlanTargetAccount
        boolean bOOLEAN;
        if (listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount) && listOmsPlanTargetAccount.size() > 0) {
            List<String> listString = new ArrayList<>();
            if (listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount) && listOmsPlanTargetAccount != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount)) {
                listString = listOmsPlanTargetAccount.stream().map(item -> item.getPlanTargetAccountId())
                        .collect(Collectors.toList());/*list-to-strings*///sourceId:162999_1
            }

            /*3-9-01批量删目标分类账户[2739]   */

            bOOLEAN = mOmsPlanTargetAccountService.batchDeletePlanTargetAccount(listString)/*vcase invoke 本地 method 方法调用;*/;


        }

//步骤2: 3-9-01查规划目标账户列表 - queryPlanTargetAccountList
        List<OmsPlanTargetAccount> listOmsPlanTargetAccount_2 = new ArrayList<>();
        QueryPlanTargetAccountListReq queryPlanTargetAccountListReq_1 = new QueryPlanTargetAccountListReq();
        queryPlanTargetAccountListReq_1.setAccountAttributionObjType("TARGET_OBJECTIVE");//sourceId:163001_1
        queryPlanTargetAccountListReq_1.setUpdateStatus("NEW");//sourceId:163004_1
        if (reqDto != null) {
            queryPlanTargetAccountListReq_1.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:163006_1
            queryPlanTargetAccountListReq_1.setBatchCode(reqDto.getBatchCode());//SimpleFieldAssign//sourceId:163005_1
        }

        /*3-9-01查指标账户列表[2738]   */
        Assert.isNull(queryPlanTargetAccountListReq_1.getAccountAttributionObjType(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查指标账户列表-账户归属对象类型不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_1.getEvaluationTemplateId(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查指标账户列表-冗余评价模板ID不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_1.getBatchCode(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查指标账户列表-批次标识不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_1.getUpdateStatus(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查指标账户列表-批次下更新状态不能为空", false);
        listOmsPlanTargetAccount_2 = mOmsPlanTargetAccountService.queryPlanTargetAccountList(queryPlanTargetAccountListReq_1)/*vcase invoke 本地 method 方法调用;*/;


//步骤3: 3-9-01批量删规划目标账户 - batchDeletePlanTargetAccount
        boolean bOOLEAN_1;
        if (listOmsPlanTargetAccount_2 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_2) && listOmsPlanTargetAccount_2.size() > 0) {
            List<String> listString_1 = new ArrayList<>();
            if (listOmsPlanTargetAccount_2 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_2) && listOmsPlanTargetAccount_2 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_2)) {
                listString_1 = listOmsPlanTargetAccount_2.stream().map(item -> item.getPlanTargetAccountId())
                        .collect(Collectors.toList());/*list-to-strings*///sourceId:163008_1
            }

            /*3-9-01批量删指标账户[2739]   */

            bOOLEAN_1 = mOmsPlanTargetAccountService.batchDeletePlanTargetAccount(listString_1)/*vcase invoke 本地 method 方法调用;*/;


        }

//步骤4: 3-9-01查规划目标账户列表 - queryPlanTargetAccountList
        List<OmsPlanTargetAccount> listOmsPlanTargetAccount_4 = new ArrayList<>();
        QueryPlanTargetAccountListReq queryPlanTargetAccountListReq_2 = new QueryPlanTargetAccountListReq();
        queryPlanTargetAccountListReq_2.setUpdateStatus("ARCHIVE");//sourceId:163013_1
        if (reqDto != null) {
            queryPlanTargetAccountListReq_2.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:163015_1
            queryPlanTargetAccountListReq_2.setBatchCode(reqDto.getBatchCode());//SimpleFieldAssign//sourceId:163014_1
        }

        /*3-9-01查本次存档规划目标账户（用于想存档）[2738]   */
        Assert.isNull(queryPlanTargetAccountListReq_2.getEvaluationTemplateId(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-冗余评价模板ID不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_2.getBatchCode(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-批次标识不能为空", false);
        Assert.isNull(queryPlanTargetAccountListReq_2.getUpdateStatus(), "D3-9撤销批次处理规划账户更新状态(公共)-3-9-01查本次存档规划目标账户（用于想存档）-批次下更新状态不能为空", false);
        listOmsPlanTargetAccount_4 = mOmsPlanTargetAccountService.queryPlanTargetAccountList(queryPlanTargetAccountListReq_2)/*vcase invoke 本地 method 方法调用;*/;


//步骤5: 3-9-01批量改规划目标账户 - batchUpdatePlanTargetAccount
        boolean bOOLEAN_2;
        if (listOmsPlanTargetAccount_4 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_4) && listOmsPlanTargetAccount_4.size() > 0) {
            List<OmsPlanTargetAccount> listOmsPlanTargetAccount_6 = new ArrayList<>();
            if (listOmsPlanTargetAccount_4 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_4) && listOmsPlanTargetAccount_4 != null && !CollectionUtil.isEmpty(listOmsPlanTargetAccount_4)) {
                listOmsPlanTargetAccount_6 = listOmsPlanTargetAccount_4.stream().map(item -> {
                    OmsPlanTargetAccount elm = BeanUtil.toBean(item, OmsPlanTargetAccount.class);
                    elm.setIsUpdate("FALSE");
                    elm.setUpdateStatus("UNCHANGED");
                    elm.setArchivngType("NOT_ARCHIVE");
                    elm.setArchiveInductionId("---");//NULL
                    elm.setArchiveTime(CommonFunctionHelper.getFutureTime());//NULL

                    return elm;
                }).collect(Collectors.toList());//objList-to-objLists//sourceId:163028_1
            }

            /*3-9-01批量改规划目标账户当前有效[3630]   */

            bOOLEAN_2 = mOmsPlanTargetAccountService.batchUpdatePlanTargetAccount(listOmsPlanTargetAccount_6)/*vcase invoke 本地 method 方法调用;*/;


        }

        UndoBatchHandlePlanAccountUpdStatusComRespDto retData = new UndoBatchHandlePlanAccountUpdStatusComRespDto();


        return retData;
    }

    //
}
